class _BridgeToApp{
    constructor(){
        try{
            if('ios' === this._dev()){
                this.connectWebViewJavascriptBridge(function(bridge) {
                    bridge.init(function(message, responseCallback) {
                        var data = { 'bridgeInit':'success' };
                        responseCallback(data)
                    });
                });
            }
        }catch (e){}
    }
    unSupport(msg = '有话好说，把神器收起来先'){
        if(!document.getElementById('_unSupport')){
            let _w = document.createElement('div');
            _w.id = '_unSupport';
            _w.innerHTML = msg;
            _w.style.textAlign = 'center';
            _w.style.color = '#999';
            document.body.appendChild(_w)
        }
    }
    _dev(){
        const ua = navigator.userAgent;
        const isIos = ua.indexOf('iPad')>0 || ua.indexOf('iPhone')>0 || ua.indexOf('iPod')>0;
        const isAndroid = ua.indexOf('Android')>0;
        const isWinPhone = ua.indexOf('Windows Phone')>0;
        const isWindows = ua.indexOf('Windows NT')>0;
        if(isIos){
            return 'ios'
        }else if(isAndroid){
            return 'android'
        }else if(isWinPhone){
            return 'wp'
        }else if(isWindows){
            return 'windows'
        }else{
            return 'other'
        }
    }
    connectWebViewJavascriptBridge(callback) {
        if (window.WebViewJavascriptBridge) {
            callback(WebViewJavascriptBridge)
        } else {
            document.addEventListener('WebViewJavascriptBridgeReady', function() {
                callback(WebViewJavascriptBridge)
            }, false)
        }
    }
    call(methodFromApp,args,callBack){
        const _d = this._dev();
        switch (_d){
            case 'ios':
                try{
                    this.connectWebViewJavascriptBridge(function(bridge) {
                        bridge.callHandler(methodFromApp, args, callBack)
                    })
                }catch(e){
                }
                break;
            case 'android':
                try{
                    if(!!args && !callBack){
                        window.AndroidWebView[methodFromApp](JSON.stringify(args));
                    }else if(!args && !callBack){
                        window.AndroidWebView[methodFromApp]();
                    }else if(!args && !!callBack){
                        this.register(methodFromApp,callBack);
                        window.AndroidWebView[methodFromApp](JSON.stringify(args));
                    }else if(!!args && !!callBack){
                        this.register(methodFromApp,callBack);
                        window.AndroidWebView[methodFromApp]();
                    }
                }catch(e){}
                break;
            case 'wp':
                this.unSupport('暂不支持windows phone');
                break;
            case 'windows':
                this.unSupport('请使用手机访问');
                break;
            default:
                this.unSupport();
                break;
        }
    }
    register(methodName,methodBLock){
        const _d = this._dev();
        switch (_d){
            case 'ios':
                try{
                    this.connectWebViewJavascriptBridge(function(bridge) {
                        bridge.registerHandler(methodName, methodBLock)
                    })
                }catch(e){}
                break;
            case 'android':
                if(!window._regMethod){
                    window._regMethod = {}
                }
                window._regMethod[methodName] = methodBLock;
                break;
            case 'wp':
                this.unSupport('暂不支持windows phone');
                break;
            case 'windows':
                this.unSupport('请使用手机访问');
                break;
            default:
                this.unSupport();
                break;
        }
    }
    _clear(){
        if('ios' !== this._dev()){
            if(window._regMethod){
                //delete window._regMethod;
            }
        }
    }
}
const BridgeToApp = new _BridgeToApp();
export default BridgeToApp